name: Build Docs
on:
  push:
    branches:
      - main
  pull_request:
    types:
      - opened
      - synchronize
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      # TODO: enable this once the README and index.md have the same content, with absolute links to a published website
      # - name: Ensure README has the same contents as docs/index.md
      #   run: diff docs/index.md README.md
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: "3.10"
      - uses: actions/cache@v2
        id: cache
        with:
          path: ${{ env.pythonLocation }}
          key: ${{ runner.os }}-python-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}
      - name: Install poetry
        if: steps.cache.outputs.cache-hit != 'true'
        shell: bash
        run: |
          python -m pip install --upgrade pip
          python -m pip install "poetry>=1.5.0"
      - name: Configure poetry
        run: python -m poetry config virtualenvs.create false
      - name: Install Dependencies
        if: steps.cache.outputs.cache-hit != 'true'
        run: python -m poetry install
      - name: Install Material for MkDocs Insiders
        run: python -m poetry run pip install git+https://${{ secrets.MK_DOCS_ACTIONS_TOKEN }}@github.com/squidfunk/mkdocs-material-insiders.git
      - uses: actions/cache@v2
        with:
          key: mkdocs-cards-${{ github.ref }}
          path: .cache
      - name: Build Docs with Insiders
        run: python -m poetry run mkdocs build --config-file mkdocs.insiders.yml
      - name: Publish to Cloudflare Pages
        uses: cloudflare/pages-action@v1
        with:
          apiToken: ${{ secrets.CF_API_TOKEN_PAGES }}
          accountId: ${{ secrets.CF_ACCOUNT_ID }}
          projectName: autochain
          directory: site/
          gitHubToken: ${{ secrets.GITHUB_TOKEN }}
          wranglerVersion: '3'
